define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type.title', title: __('Type.title'), operate: 'LIKE'},
                        {field: 'username', title: '收件人'},
                        {field: 'phone', title: '手机号'},
                        {field: 'remarks', title: '备注', formatter: function (value, row, index) {
                                if(value){
                                    return "<div style='max-width: 200px;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;' title="+ value +" >"+value+"</div>";
                                }else{
                                    return "-";
                                }
                            }},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('修改状态'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons:[
                                {
                                    name: 'ajax',
                                    text: '',
                                    title: '复制游戏名称',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-copy',
                                    url: 'order_goods/get_name_list?id={id}',
                                    confirm: false,
                                    success: function (data, ret) {
                                        let text = ret.code_number;
                                        copyText(text);
                                        layer.msg('复制成功');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return false;
                                    },
                                    error: function (data, ret) {

                                        return false;
                                    }
                                },{
                                    name: 'ajax',
                                    text: '',
                                    title: '复制游戏码',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-copy',
                                    url: 'order_goods/get_number?id={id}',
                                    confirm: false,
                                    success: function (data, ret) {
                                        let text = ret.code_number;
                                        copyText(text);
                                        layer.msg('复制成功');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        return false;
                                    },
                                    error: function (data, ret) {

                                        return false;
                                    }
                                },
                                {
                                    name:'viewdetail',
                                    text:'查看游戏',
                                    title:'游戏详情',
                                    classname:'btn btn-xs btn-primary btn-dialog',
                                    icon:'fa fa-angellist',
                                    url:'order_goods/index?id={id}',
                                    visible: function (){
                                        return true;
                                    }
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $('#c-type_id').val(3);
        },
        get_detail: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    function copyText(text) {
        var textarea = document.createElement("textarea"); //创建临时的textarea元素
        textarea.value = text; //将要复制的内容赋值给textarea
        document.body.appendChild(textarea); //将textarea添加到页面上
        textarea.select(); //选中textarea中的内容
        try {
            var successful = document.execCommand('copy'); //执行复制命令
            if (successful) {
                console.log('成功复制！');
            } else {
                console.log('无法复制！');
            }
        } catch (err) {
            console.error('无法复制！', err);
        } finally {
            document.body.removeChild(textarea); //移除临时的textarea元素
        }
    }

    return Controller;
});
